<template>
	<div>

		<el-card>
			<el-form ref="ruleForm" :rules="rules" :model="form" label-width="80px">
				<el-row>
					<el-col :span="12">
						<el-form-item label="名称" prop="name">
							<el-input v-model="form.name" type="text" placeholder="请输入名称"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="标题" prop="title">
							<el-input v-model="form.title" type="text" placeholder="请输入标题"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="现价" prop="price">
							<el-input v-model="form.price" placeholder="请输入现价"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="市场价" prop="oldprice">
							<el-input v-model="form.oldprice" type="Number" placeholder="请输入市场价"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="运费" prop="exp">
							<el-input v-model="form.exp" type="Number" placeholder="请输入运费"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="库存" prop="store">
							<el-input v-model="form.store" type="Number" placeholder="请输入库存"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="积分" prop="integral">
							<el-input v-model="form.integral" type="Number" placeholder="请输入积分"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="分类" prop="classify">
							<el-select v-model="form.classify" placeholder="请选择分类">
								<div v-for="(item,index) in ClassifyList" :key="index">
									<el-option :label="item.name" :value="item.id"></el-option>
								</div>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="封面图" prop="cover">
					<el-input v-model="form.cover" v-if="false"></el-input>
					<upload ref="cover" :limit="1" :tempImg="form.cover"></upload>
				</el-form-item>
				<el-form-item label="轮播图" prop="images">
					<el-input v-model="form.images" v-if="false"></el-input>
					<upload ref="images" :limit="3" :tempImg="form.images"></upload>
				</el-form-item>
				<el-form-item label="文本">
					<editor ref="editor" :detail="form.detail"></editor>
				</el-form-item>
				<el-form-item>
					<el-button size="medium" type="primary" @click="submitForm('ruleForm')" :loading="loading">提交
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>

	</div>
</template>

<script>
	import editor from '@/components/editor/index';
	import upload from '@/components/upload.vue';
	import allRule from '@/common/rule.js';
	export default {
		components: {
			editor,
			upload
		},
		data() {
			return {
				loading: false,
				form: {
					id: "",
					name: '',
					title: "",
					cover: [],
					images: [],
					store: "",
					integral: "",
					price: "",
					oldprice: "",
					exp: ""

				},
				rules: allRule,
				ClassifyList: []
			}
		},
		created() {
			this.getClassify()
			if(this.$route.query){
				this.jiazai(this.$route.query)
			}
		},
		methods: {
			getClassify() {
				this.axios.post("/api/index/findMallClassify").then(res => {
					this.ClassifyList = res.data;
				}).catch(error => {

				})
			},
			jiazai(e) {
				this.axios.post("/api/index/findOrdinaryMall", {
					id:e.id,
				}).then(res => {
				   res.cover=[res.cover];
					this.form=res;
				}).catch(error => {
			
				})
			},
			submitForm(formName) {
				this.form.images = this.$refs.images.imageListString;
				this.form.cover = this.$refs.cover.imageListString;
				
				this.form.detail=this.$refs.editor.content;
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.axios.post("/admin/user/addOrdinarymall", this.form).then(res => {
							this.$message.success({
								message: "添加成功",
								onClose: (res) => {
									this.$router.replace("/OrdinaryMall")
								}
							})
						}).catch(error => {

						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
		}
	}
</script>

<style>
</style>
